package com.leapp.sk.ui.adapter

import android.Manifest
import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.support.v4.app.ActivityCompat
import android.view.View
import com.example.baselibrary.db.table.AccountTable
import com.example.baselibrary.util.SKPermissionUtils
import com.example.mvp.adapter.BaseLibAdapter
import com.example.mvp.binder.BaseDataBinder
import com.leapp.sk.R
import com.leapp.sk.app.SKApplication
import com.leapp.sk.bean.ConcatTeacherDataEntity
import com.leapp.sk.binder.BaseBinder
import com.leapp.sk.ui.activity.im.SingleChatActivity
import com.example.util.finallist.MESSAGE_LIST_ID
import com.example.util.finallist.TECHER_NICK
import com.leapp.sk.viewdelegate.adapter.personal.ContanctTecherAdapterDelegate

/**
 * 联系老师适配器
 * @author  chaohaipeng  on 2018/8/8.
 */
class ContactTecherAdapter (context: Activity, mlist: List<ConcatTeacherDataEntity>) : BaseLibAdapter<ContanctTecherAdapterDelegate, ConcatTeacherDataEntity>(context, mlist){

    /**
     * 数据
     */
    val mList = mlist
    /**
     * 上下文
     */
    val mContext = context

    override fun bindEvenListener(viewDelegate: ContanctTecherAdapterDelegate?, position: Int) {
        viewDelegate?.setOnClickListener(View.OnClickListener {
            val get = mList.get(position)
            val intent: Intent = Intent(mContext, SingleChatActivity::class.java)
            intent.putExtra(MESSAGE_LIST_ID, get.phone)
            intent.putExtra(TECHER_NICK, get.nickName)
            mContext.startActivity(intent)
        }, R.id.iv_act_message_contanct)
        viewDelegate?.setOnClickListener(View.OnClickListener {
            val get = mList.get(position)
            callPhone(get?.phone!!)
        }, R.id.iv_act_phone_contanct)
    }
    override fun notifyView(viewDelegate: ContanctTecherAdapterDelegate?, position: Int) {
        val get = mList.get(position)
        val accountTable = AccountTable()
        accountTable.avatar=get?.avatar
        accountTable.nick=get?.nickName
        accountTable.account=get?.phone
        viewDelegate?.setData(mContext,mList.get(position))

    }

    override fun getDelegateClass(): Class<ContanctTecherAdapterDelegate> {
        return ContanctTecherAdapterDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }
    @SuppressLint("MissingPermission")
            /**
             * 拨打电话（直接拨打电话）
             * @param phoneNum 电话号码
             */
    fun callPhone(phoneNum: String) {
        //用intent启动拨打电话
        if (SKPermissionUtils.getInstance().setCallPhonePermission(mContext, SKApplication.getInstance())) {
            val intent = Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum))
            if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                return
            }
           mContext.startActivity(intent)
        }
    }
}